import React, {useEffect} from 'react';
import {useTranslation} from 'react-i18next';

export interface ConfigProviderProps {
    locale?: string;
    theme?: 'light' | 'dark' | string;
    children: React.ReactNode;
}

const ConfigProvider: React.FC<ConfigProviderProps> = ({
                                                           locale: localeProp = 'zh_cn',
                                                           theme = 'light',
                                                           children
                                                       }) => {
    const {i18n} = useTranslation();

    // 监听 locale 变化并更新 i18n
    useEffect(() => {
        if (i18n && typeof i18n.changeLanguage === 'function' && i18n.language !== localeProp) {
            i18n.changeLanguage(localeProp).catch((error) => {
                console.warn('语言切换失败:', error);
            });
        }
    }, [localeProp, i18n]);

    // 监听 theme 变化并更新 document 属性
    useEffect(() => {
        document.documentElement.setAttribute('data-theme', theme);
    }, [theme]);

    return <>{children}</>;
};

export default ConfigProvider;
